﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.Packaging;
using System.Threading;

namespace intelliEssay.Interop.DocumentElementPropertyCache
{
    internal static class WordDocPropertyCache
    {
        static ReaderWriterLockSlim syncGate = new ReaderWriterLockSlim(LockRecursionPolicy.NoRecursion);
        static Dictionary<WordprocessingDocument, Dictionary<string, dynamic>> _ProeprtyCache = new Dictionary<WordprocessingDocument, Dictionary<string, dynamic>>();
        public static void StoreProperty(WordprocessingDocument wd, string propertyName, dynamic value)
        {
            syncGate.EnterWriteLock();
            if (!_ProeprtyCache.ContainsKey(wd))
            {
                _ProeprtyCache.Add(wd, new Dictionary<string, dynamic>());
            }
            if (!_ProeprtyCache[wd].ContainsKey(propertyName))
            {
                _ProeprtyCache[wd].Add(propertyName, value);
            }
            syncGate.ExitWriteLock();
        }
        public static dynamic GetProperty(WordprocessingDocument wd, string propertyName)
        {
            dynamic value = null;
            syncGate.EnterReadLock();
            if (_ProeprtyCache.ContainsKey(wd))
            {
                if (_ProeprtyCache[wd].ContainsKey(propertyName))
                {
                    value = _ProeprtyCache[wd][propertyName];
                }
            }
            syncGate.ExitReadLock();
            return value;
        }
    }
}
